package com.relife.core.spring;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.relife.common.utils.StringUtil;
import org.apache.log4j.Logger;


/**
 * 
 * @author WU
 * @date 2016年6月15日 上午9:42:35
 */
public class DateEditorSupport extends PropertyEditorSupport {
	private static final Logger log = Logger.getLogger(DateEditorSupport.class);

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (StringUtil.isNotEmpty(text)) {
			String sdf = "yyyy-MM-dd HH:mm:ss:SSS";
			try {
				sdf = sdf.substring(0, text.length());
				setValue(new SimpleDateFormat(sdf).parse(text));
			} catch (ParseException e) {
				log.error("日期【" + text + "】与格式【" + sdf + "】不匹配");
				IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + e.getMessage());
				iae.initCause(e);
				throw iae;
			}
		} else {
			setValue(null);
		}
	}
}
